// StatusBar.cpp : Implementation of CStatusBar

#include "stdafx.h"

#include "StatusBar.h"

// CStatusBar

CStatusBar * CStatusBar::m_pThis = NULL;

STDMETHODIMP CStatusBar::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IStatusBar
	};


	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}

	return S_FALSE;
}


LRESULT CStatusBar::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	DefWindowProc();

	PAINTSTRUCT ps = {0};
	HDC hdc = NULL;
	HPEN hPen;
	HDC hdcMem = NULL;
	HBITMAP hBitmap = NULL;
	HBITMAP hOldBitmap = NULL;
	RECT rcRect = {0};
	CRect rcItem;


	hdc = ::BeginPaint(m_hWnd, &ps);
	GetClientRect(&rcRect);

	hdcMem = ::CreateCompatibleDC(hdc);
	hBitmap = ::CreateCompatibleBitmap(hdcMem, rcRect.right, rcRect.bottom);
	hOldBitmap = (HBITMAP)SelectObject(hdcMem, hBitmap);
	hPen = CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_3DSHADOW));

	HPEN hOldPen = (HPEN)SelectObject(hdcMem, hPen);
	HBRUSH hOldBrush = (HBRUSH)SelectObject(hdcMem, (HBRUSH)AtlGetStockBrush(NULL_BRUSH));

	for ( int i = 0; i < m_nPanes; i++ )
	{
		GetRect (i, rcItem);
		Rectangle (hdcMem, rcItem.left, rcItem.top, rcItem.right, rcItem.bottom); 
	}

	SelectObject(hdcMem, hOldBrush);
	SelectObject(hdcMem, hOldPen);

	BitBlt(hdc, 0, 0, rcRect.right, rcRect.bottom, hdcMem, 0, 0, SRCCOPY);

	::EndPaint(m_hWnd, &ps);

	return 0;
}


void CStatusBar::SetPaneWidths(int* arrWidths, int nPanes)
{
	ATLASSERT( m_nPanes == nPanes);
	// find the size of the borders
	int arrBorders[3];
	GetBorders(arrBorders);

	// calculate right edge of default pane (0)
	arrWidths[0] += arrBorders[2];
	for (int i = 1; i < nPanes; i++)
		arrWidths[0] += arrWidths[i];

	// calculate right edge of remaining panes (1 thru nPanes-1)
	for (int j = 1; j < nPanes; j++)
		arrWidths[j] += arrBorders[2] + arrWidths[j - 1];

	// set the pane widths
	SetParts(m_nPanes, arrWidths); 
}


STDMETHODIMP CStatusBar::SetPanes(safStatusBarItemInfo* nArray, int nCount)
{
	//ATLASSERT(NULL != 0);
	int *_nArray = NULL;
	_StatusBarCol::_ClassItem *pStatusBarItem = NULL;
	 
	if (nCount > 0)
	{
		_nArray = new int[nCount];
		for (int nItem=0; nItem<nCount; nItem++)
		{
			pStatusBarItem = new _StatusBarCol::_ClassItem;
			pStatusBarItem->nID = nArray[nItem].nID;
			pStatusBarItem->nWidth = nArray[nItem].nWidth;
			m_coll.push_back(pStatusBarItem);

			_nArray[nItem] = nArray[nItem].nID;
			//_nArray++;
		}

		CMultiPaneStatusBarCtrl::SetPanes(_nArray, nCount, true);
		SetPaneWidths(_nArray, nCount);

		if (::IsWindow(m_hWnd))
		{
			UpdatePanesLayout();
		}

		delete []_nArray;
	}

	return S_OK;
}


STDMETHODIMP CStatusBar::put_LockAero(VARIANT_BOOL newLockAero)
{
	m_bLockAero = newLockAero;
	return S_OK;
}


STDMETHODIMP CStatusBar::get_LockAero(VARIANT_BOOL *pLockAero)
{
	*pLockAero = m_bLockAero;
	return S_OK;
}


BOOL CStatusBar::UpdatePanesLayout()
{
	_StatusBarCol::_ClassItem *pStatusBarItemInfo = NULL;
	_StatusBarCol::CollType::const_iterator _Iter;

	_Iter = m_coll.begin();
	RECT rcRect = {0};
	int nWidth = 0;
	if (NULL != m_hWnd && _Iter!=m_coll.end())
	{
		this->GetClientRect(&rcRect);
		if (m_bLockAero == VARIANT_TRUE)
		{
			for (_Iter = m_coll.begin(); _Iter!=m_coll.end(); _Iter++)
			{
				pStatusBarItemInfo = *_Iter;
				nWidth += pStatusBarItemInfo->nWidth;
			}				
			pStatusBarItemInfo = *m_coll.begin();
			pStatusBarItemInfo->nWidth += rcRect.right - nWidth;
		}

		for (_Iter = m_coll.begin(); _Iter!=m_coll.end(); _Iter++)
		{
			pStatusBarItemInfo = (*_Iter);
			this->SetPaneWidth(pStatusBarItemInfo->nID, pStatusBarItemInfo->nWidth);
		}
	}

	return TRUE;
}
